package example.dawangba.com.z09315222_finalandroidhomework;

import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SeekBar;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class PlayMusicActivity extends Activity {

    ListView listView;
    List<String> listName,listPath;
    MediaPlayer mediaPlayer;
    SeekBar seekBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_music);

        listView = (ListView) findViewById(R.id.listView);
        //指定要播放的音乐文件的路径
        File path = new File(Environment.getExternalStorageDirectory()+"/Music");
        listName = new ArrayList<String>();
        listPath = new ArrayList<String>();
        getMusicLists(path);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(PlayMusicActivity.this,
                android.R.layout.simple_list_item_1,listName);
        listView.setAdapter(adapter);

        mediaPlayer = new MediaPlayer();
        seekBar = (SeekBar) findViewById(R.id.seekBar);
        setListener();

        //拖动SeekBar改变歌曲进度
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //用seekTo直接跳转进度条进度到seekBar当前拖动到的进度位置
                mediaPlayer.seekTo(seekBar.getProgress());
            }
        });
    }

    //listView的单击事件，根据点击到的item来播放对应的歌曲
    private void setListener() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                try {
                    if( mediaPlayer != null){
                        mediaPlayer.stop();
                        mediaPlayer.reset();
                    }
                    //根据position来播放点击到的歌曲
                    mediaPlayer.setDataSource(PlayMusicActivity.this, Uri.parse(listPath.get(position)));
                    //mediaPlayer的准备过程
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                    //seekBar获取进度条最大值、设置进度条初始进度为0
                    seekBar.setMax(mediaPlayer.getDuration());
                    seekBar.setProgress(0);
                    ChangeSeekBar();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    //进度条当前位置的获取
    private void ChangeSeekBar() {
        final Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                //用msg.arg1来传递进度值
                seekBar.setProgress(msg.arg1);
            }
        };
        Thread t = new Thread(){
            @Override
            public void run() {
                super.run();
                while (seekBar.getProgress() < seekBar.getMax()){
                    //获取当前进度
                    int progress = mediaPlayer.getCurrentPosition();
                    Message msg = handler.obtainMessage();
                    //设置msg.arg1的值为progress,实现进度获取
                    msg.arg1 = progress;
                    handler.sendMessage(msg);

                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        t.start();
    }

    //将获取到的音乐文件的名字显示在listView中
    private void getMusicLists(File path) {
        if (path != null){
            if (path.isDirectory()){
                File[] files = path.listFiles();
                for (int i = 0; i < files.length; i++) {
                    getMusicLists(files[i]);
                }
            } else {
                String musicName = path.getName();
                String musicPath = path.getAbsolutePath();
                if (musicName.endsWith(".mp3")){
                    listName.add(musicName);
                    listPath.add(musicPath);
                }
            }
        }
    }
}
